package com.jojo.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jojo.domain.IndexImg;
import com.jojo.model.Result;
import com.jojo.service.IndexImgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 轮播图业务控制层
 */
@Api("轮播图接口管理")
@RequestMapping("admin/indexImg")
@RestController
public class IndexImgController {
    @Autowired
    private IndexImgService indexImgService;

    @ApiOperation("多条件分表查询轮播图")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('admin:indexImg:page')")
    public Result<Page<IndexImg>> loadIndexImgPage(@RequestParam Integer current,
                                                   @RequestParam Integer size,
                                                   @RequestParam(required = false) Integer status){
        Page<IndexImg> page = new Page<>(current,size);
        page = indexImgService.page(page,new LambdaQueryWrapper<IndexImg>()
                .eq(ObjectUtil.isNotNull(status),IndexImg::getStatus,status)
                .orderByDesc(IndexImg::getSeq)
        );
        return Result.success(page);
    }

    @ApiOperation("新增轮播图")
    @PostMapping
    @PreAuthorize("hasAuthority('admin:indexImg:save')")
    public Result<?> load(@RequestBody IndexImg indexImg){
        Boolean saved = indexImgService.saveIndexImg(indexImg);
        return Result.handle(saved);
    }

    @ApiOperation("根据标识查询轮播图信息")
    @GetMapping("info/{indexImgId}")
    @PreAuthorize("hasAuthority('admin:indexImg:info')")
    public Result<?> loadIndexImgInfoById(@PathVariable Long indexImgId){
        IndexImg indexImg = indexImgService.queryIndexImgInfoById(indexImgId);
        return Result.success(indexImg);
    }

    @ApiOperation("修改轮播图内容")
    @PutMapping
    @PreAuthorize("hasAuthority('admin:indexImg:update')")
    public Result<?> modifyIndexImg(@RequestBody IndexImg indexImg){
        Boolean modified = indexImgService.modifyIndexImg(indexImg);
        return Result.handle(modified);
    }

    @ApiOperation("批量删除轮播图")
    @DeleteMapping
    @PreAuthorize("hasAuthority('admin:indexImg:delete')")
    public Result<?> removeIndexImg(@RequestBody List<Long> ids){
        Boolean removed = indexImgService.removeIndexImg(ids);
        return Result.handle(removed);
    }

}
